<template>
  <div class="app-container-item">
    <div class="form-main">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8" :xs="24">
            <el-form-item label="小区名称" prop="communityName">
              <el-input v-model="form.communityName" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="车场名称" prop="parkingName">
              <el-input v-model="form.parkingName"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="车场类型" prop="parkingType">
              <el-select v-model="form.parkingType" placeholder="请选择车场类型">
                <el-option
                  v-for="dict in dict.parking_type"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="车场面积" prop="parkingArea">
              <el-input v-model="form.parkingArea" placeholder="请输入车场面积"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="排序" prop="orderNum">
              <el-input v-model="form.orderNum" placeholder="请输入排序"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="车位数量" prop="carportTotalNum">
              <el-input v-model="form.carportTotalNum" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="已用数" prop="usedNum">
              <el-input v-model="form.usedNum" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="未用数" prop="unusedNum">
              <el-input v-model="form.unusedNum" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="dialog-footer" style="text-align: center;">
      <el-button type="primary" plain @click="submitForm"  v-hasPermi="['property:parking:edit']">保 存</el-button>
    </div>
  </div>
</template>

<script>
  import { getParking, updateParking ,getParkingCarport } from "@/api/property/parking";

  export default {
    name:"parkingForm",
    dicts:['parking_type'],
    data() {
      return {
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          parkingName: [
            {required: true, message: "车场名称不能为空", trigger: "blur"}
          ],
          parkingType: [
            {required: true, message: "车场类型不能为空", trigger: "change"}
          ],
        }
      };
    },
    watch: {
      currentNode(v) {
        this.initForm()
      }
    },
    created() {
      this.initForm()
    },
    methods: {
      initForm() {
        this.reset();
        if (this.currentNode.modelType == 'park') {
          getParking(this.currentNode.id).then(response => {
            this.form = Object.assign(response.data,{carportTotalNum:0,usedNum:0,unusedNum:0});
            getParkingCarport(this.currentNode.id).then(res=>{
              this.form.carportTotalNum = res.data.carportTotalNum
              this.form.usedNum = res.data.usedNum
              this.form.unusedNum = res.data.unusedNum
            })
          });
        }
      },
      // 表单重置
      reset() {
        this.form = {
          parkingId: null,
          communityId: null,
          parkingNum: null,
          parkingName: null,
          carportNum: null,
          parkingType: null,
          parkingArea: null,
          carportTotalNum:null,
          usedNum:null,
          unusedNum:null,
          communityName:null,
          orderNum:null
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateParking(this.form).then(response => {
              this.msgSuccess("修改车场成功");
              this.open = false;
              this.$emit("ok")
            });
          }
        });
      }
    },
    props: {
      currentNode: {
        type: Object,
        default: () => {
        }
      },
    }
  };
</script>
